<?php

declare(strict_types=1);

namespace App\VO\Homepage;

use App\BO\ConvertibleCard\Constant as ConvertibleCardConstant;
use App\BO\HomepageBO;
use App\VO\Interfaces\IVODetail;

class VODetail implements IVODetail
{
    public $hot                  = [];
    public $jiadianChuman        = [];
    public $reginal              = [];
    public $recommend            = [];
    public $combo                = [];
    public $location             = [];
    public $ad                   = [];
    public $isDouble11           = 0;
    public $showRemindPage       = 0;
    public $sysMain              = [];
    public $sysDefaults          = [];
    public $cityInfo             = [];
    public $citySettings         = [];
    public $isGuessLocation      = 0;
    public $hotItemShowCount     = 4;
    public $extraValues          = [];
    public $tel                  = ConvertibleCardConstant::CUSTOMERSERVICE_PHONENUMBER;

    public function __construct($data)
    {
        $this->hot              = $data['hot'] ? $this->hotDataHandle($data['hot']) : [];
        $this->jiadianChuman    = $data['jiadianChuman'] ?? [];
        $this->reginal          = $data['reginal'] ?? [];
        $this->recommend        = $data['recommend'] ?? [];
        $this->combo            = $data['combo'] ?? [];
        $this->location         = $data['location'] ?? [];
        $this->ad               = $data['ad'] ? $this->advertDataAppend($data['ad']) : [];
        $this->isDouble11       = $data['isDouble11'] ?? 0;
        $this->showRemindPage   = $data['showRemindPage'] ?? 0;
        $this->sysMain          = $data['sysMain'] ?? [];
        $this->sysDefaults      = $data['sysDefaults'] ?? [];
        $this->cityInfo         = $data['cityInfo'] ?? [];
        $this->citySettings     = $data['citySettings'] ?? [];
        $this->isGuessLocation  = $data['isGuessLocation'] ?? 0;
        $this->hotItemShowCount = HomepageBO::HOT_ITEM_SHOW_COUNT;
        $this->extraValues      = $data['extraValues'] ?? [];
    }

    private function advertDataAppend($ad)
    {
        if ($ad) {
            foreach ($ad as $key => $item) {
                //匹配产品ID
                $ad[$key]['product_id'] = 0;
                if (preg_match('/productsShow\/(\d+)$/', $item['advert_url'], $matches) && isset($matches[1]) && $matches[1]) {
                    $ad[$key]['product_id'] = $matches[1];
                }
            }
        }
        return $ad;
    }

    private function hotDataHandle($hot)
    {
        if ($hot) {
            foreach ($hot as $key => $item) {
                if ($item['is_show'] == 0) {
                    unset($hot[$key]);
                    continue;
                }
                $hot[$key]['product_id'] = $item['id'];
            }
            $hot = array_values($hot);
        }
        return $hot;
    }
}
